package com.sevenGroup.pawnshop.Utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

//调用此方法可以复制对象属性到另一个对象
public class CopyProperties {
//    复制一个
    public static <T>T copyOne(Object object,Class<T> tClass){
        T t =null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(object,t);
        return t;
    }
//    复制多个
    public static <T>List<T> copyList(List objectList,Class<T> tClass){
        List<T> list=new ArrayList<>();
        for (Object o : objectList) {
            T t=null;
            try {
                t = tClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtils.copyProperties(o,t);
            list.add(t);
        }
        return list;
    }
}
